﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;

namespace CrazyWorship1
{
    [XmlRoot("SongInfoRoot")]
    public class SongInfo : INotifyPropertyChanged, IXmlSerializable
    {
        public SongInfo()
        {
            Version = Guid.NewGuid();
            Id = Guid.NewGuid();
        }
        public static SongInfo Load(string file)
        {
            FileStream stream = new FileStream(file, FileMode.OpenOrCreate);
            XmlSerializer x = new XmlSerializer(typeof(SongInfo));

            SongInfo si = (SongInfo)x.Deserialize(stream);
            stream.Close();
            return si;
        }
        public string SongFileName
        {
            get
            {
                return AssetManager.SongFolder + "/" + Id.ToString() + "//song.xml";
            }
        }
        public string SongInfoFileName
        {
            get
            {
                return AssetManager.SongFolder + "/" + Id.ToString() + "//songinfo.xml";
            }
        }

        Status _status;
        Guid _Id;
        Guid _version;
        string _name;
        public Guid Id
        {
            get
            {
                return _Id;
            }
            set
            {
                _Id = value;
                NotifyPropertyChanged("FileName");
            }
        }
        public Status Status
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
                NotifyPropertyChanged("Status");
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
        string _songname;
        public string SongName
        {
            get
            {
                return _songname;
            }
            set
            {
                _songname = value;
                NotifyPropertyChanged("SongName");
            }
        }
        public string Author
        {
            get
            {
                return _author;
            }
            set
            {
                _author = value;
                NotifyPropertyChanged("Author");
            }
        }
        public string Copyright
        {
            get
            {
                return _copyright;
            }
            set
            {
                _copyright = value;
                NotifyPropertyChanged("Copyright");
            }
        }
        string _copyright, _author, _ccli;
        public string CCLI
        {
            get
            {
                return _ccli;
            }
            set
            {
                _ccli = value;
                NotifyPropertyChanged("CCLI");
            }
        }

        public Guid Version
        {
            get
            {
                return _version;
            }
            set
            {
                _version = value;
                NotifyPropertyChanged("Version");
            }
        }
        public int Tries = 0;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.ReadStartElement("SongInfoRoot");


            this._Id = new Guid((string)reader.ReadElementContentAsObject("_id", ""));
            this._name = (string)reader.ReadElementContentAsObject("_name", "");

            this._version = new Guid((string)reader.ReadElementContentAsObject("_version", ""));
            this.Status = Status.NotSynced;
            this.SongName = (string)reader.ReadElementContentAsObject("_songname", "");
            this.Author = (string)reader.ReadElementContentAsObject("_author", "");
            this.Copyright = (string)reader.ReadElementContentAsObject("_copyright", "");
            this.CCLI = (string)reader.ReadElementContentAsObject("_ccli", "");
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("_id");
            writer.WriteValue(this._Id.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_name");
            writer.WriteValue(this._name);
            writer.WriteEndElement();


            writer.WriteStartElement("_version");
            writer.WriteValue(this._version.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_songname");
            writer.WriteValue(this._songname.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_author");
            writer.WriteValue(this._author.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_copyright");
            writer.WriteValue(this._copyright.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_ccli");
            writer.WriteValue(this._ccli.ToString());
            writer.WriteEndElement();

        }
        #endregion
    }
}
